package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
	
	@Autowired
	private SetmealMapper setmealMapper;
	@Autowired
	private SetmealDishMapper setmealDishMapper;
	@Autowired
	private DishMapper dishMapper;
	
	/**
	 * 分页查询
	 *
	 * @param setmealPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult getSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
		// 插件自动计算分页
		PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
		// 获取返回所有信息
		Page<SetmealVO> setmeal = setmealMapper.getSetmeal(setmealPageQueryDTO);
		return new PageResult(setmeal.getTotal(), setmeal.getResult());
	}
	
	/**
	 * 根据套餐分类id查询
	 *
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<Setmeal> getSetmealCategoryId(Long categoryId) {
		List<Setmeal> setmeals = setmealMapper.getSetmealCategoryId(categoryId);
		return setmeals.stream().filter(e -> e.getStatus() == 1).collect(Collectors.toList());
	}
	
	/**
	 * 添加套餐
	 *
	 * @param setmealDTO
	 */
	@Override
	public void addSetmeal(SetmealDTO setmealDTO) {
		// 新增
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO, setmeal);
		setmealMapper.addSetmeal(setmeal);
		// 新增对应菜品
		List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
		if (setmealDishes != null && !setmealDishes.isEmpty()) {
			setmealDishes.forEach(e -> e.setSetmealId(setmeal.getId()));
			setmealDishMapper.addSetmealDish(setmealDishes);
		}
	}
	
	/**
	 * 根据id查询
	 *
	 * @param id
	 * @return
	 */
	@Override
	public SetmealVO getSetmealId(Long id) {
		SetmealVO setmealVO = new SetmealVO();
		BeanUtils.copyProperties(setmealMapper.getSetmealId(id), setmealVO);
		setmealVO.setSetmealDishes(setmealDishMapper.getSetmealDishId(id));
		return setmealVO;
	}
	
	/**
	 * 起售/停售套餐
	 *
	 * @param id
	 * @param status
	 */
	@Override
	public void putSetmealStatus(Long id, Integer status) {
		if (status.equals(StatusConstant.ENABLE)) {
			List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishId(id);
			setmealDishes.forEach(e -> {
				Integer dishStatus = dishMapper.getDishId(e.getDishId()).getStatus();
				if (dishStatus.equals(StatusConstant.DISABLE)) {
					throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
				}
			});
		}
		setmealMapper.putSetmealStatus(id, status, LocalDateTime.now(), BaseContext.getCurrentId());
	}
	
	/**
	 * 删除套餐
	 *
	 * @param ids
	 */
	@Override
	public void deleteSetmeal(List<Long> ids) {
		if (ids != null && !ids.isEmpty()) {
			// 删除时查看是否在起售
			ids.forEach(e -> {
				Setmeal setmeal = setmealMapper.getSetmealId(e);
				if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
					throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
				}
			});
			setmealMapper.deleteSetmeal(ids);
			setmealDishMapper.deleteSetmealDish(ids);
			return;
		}
		throw new DeletionNotAllowedException(MessageConstant.SETMEAL_NOT_CHOICE);
	}
	
	/**
	 * 修改套餐
	 *
	 * @param setmealDTO
	 */
	@Override
	public void putSetmeal(SetmealDTO setmealDTO) {
		Setmeal setmeal = new Setmeal();
		BeanUtils.copyProperties(setmealDTO, setmeal);
		setmealMapper.putSetmeal(setmeal);
		// 删除数据
		setmealDishMapper.deleteSetmealDish(Collections.singletonList(setmeal.getId()));
		// 新增对应菜品
		List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
		if (setmealDishes != null && !setmealDishes.isEmpty()) {
			setmealDishes.forEach(e -> e.setSetmealId(setmeal.getId()));
			setmealDishMapper.addSetmealDish(setmealDishes);
		}
	}
}
